﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using SdmxMl.Msg;
using SdmxMl.Common;


namespace SdmxMl.Helper
{
    /// <summary> Helper class for accesing XML content </summary>
    public class Xmlhelper
    {
        /// <summary> Retrieves specified attribute value string</summary>
        /// <param name="nodeArtefact">the node holding attributes</param>
        /// <param name="attName">the attribute name</param>
        /// <returns>Attribute string value or empty string if not found</returns>
        public static string GetAtt(XmlNode nodeArtefact, string attName)
        {
            XmlNode ndAtt = nodeArtefact.Attributes.GetNamedItem(attName);
            if (ndAtt != null)
                return ndAtt.Value;
            return string.Empty;
        }

        public static string GetNodeText(XmlNode nodeparent, string query, XmlNamespaceManager nsmgr)
        {
             XmlNode nodeName = nodeparent.SelectSingleNode(query, nsmgr);
             if (nodeName != null)
                 return nodeName.InnerText;
             return string.Empty;
        }

        public static SdmxNamespaceManager CreateSdmxNamespace(XmlDocument xDoc)
        {
            SdmxVersion sdmvVers = SdmxVersion.SDMX_20;
            if (xDoc.DocumentElement.NamespaceURI == "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message")
                sdmvVers = SdmxVersion.SDMX_21;

            SdmxNamespaceManager nsmgr = new SdmxNamespaceManager(sdmvVers, xDoc.NameTable);
            return nsmgr;
        }

        public static void WriteElementIfExist(XmlTextWriter xw, string prefix, string tag, string val)
        {
            if (string.IsNullOrEmpty(val) == false)
                xw.WriteElementString(prefix, tag, null, val);
        }

        public static void WriteAttributeIfExist(XmlTextWriter xw, string prefix, string tag, string val)
        {
            if (string.IsNullOrEmpty(val) == false)
                xw.WriteAttributeString(prefix, tag, null, val);
        }

        public static void WriteAttributeIfExist(XmlTextWriter xw, string tag, string val)
        {
            if (string.IsNullOrEmpty(val) == false)
                xw.WriteAttributeString(tag, val);
        }

        public static List<XmlNode> SelectNodesElement(XmlDocument xDoc, string query, SdmxNamespaceManager nsmgr)
        {
            List<XmlNode> list = new List<XmlNode>();
            XmlNodeList nodeList = xDoc.SelectNodes(query, nsmgr);
            foreach (XmlNode n in nodeList)
            {
                if (n.NodeType == XmlNodeType.Element)
                    list.Add(n);
            }

            return list;
        }

        public static List<XmlNode> SelectNodesElement(XmlNode node, string query, SdmxNamespaceManager nsmgr)
        {
            List<XmlNode> list = new List<XmlNode>();
            XmlNodeList nodeList = node.SelectNodes(query, nsmgr);
            foreach (XmlNode n in nodeList)
            {
                if (n.NodeType == XmlNodeType.Element)
                    list.Add(n);
            }

            return list;
        }

        public static string XsDateTime(string date)
        {
            string dtStr = string.Empty;
            if (string.IsNullOrEmpty(date) == false)
            {
                try
                {
                    DateTime dt = Convert.ToDateTime(date);
                    //ISO 8601 
                    //dtStr = dt.ToString("o");

                    dtStr = dt.ToString("yyyy-MM-dd'T'HH:mm:ss");
                }
                catch { }
            }

            return dtStr;
        }

    }
}
